package com.guinea.core;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import com.guinea.account.entity.Dictionary;

public class DictionaryTag extends TagSupport {

	private static final long serialVersionUID = -774840953985543612L;
	
	String type;
	String var;
	

	public String getType() {
		return type;
	}


	public void setType(String type) {
		this.type = type;
	}


	public String getVar() {
		return var;
	}


	public void setVar(String var) {
		this.var = var;
	}


	public int doEndTag() throws JspException{
		List rootDictionary = (List)pageContext.getServletContext().getAttribute("rootDictionary");
		List leafDictionary = (List)pageContext.getServletContext().getAttribute("leafDictionary");
		List result = new ArrayList();
		Long parentId = null;
		if(type != null && var != null && rootDictionary != null && leafDictionary != null)
		for (Iterator iter = rootDictionary.iterator(); iter.hasNext();) {
			Dictionary d = (Dictionary) iter.next();
			if(d != null && type.equalsIgnoreCase(d.getCode())){
				parentId = d.getId();
				break;
			}
		}
		if(parentId != null)
		for (Iterator iter = leafDictionary.iterator(); iter.hasNext();) {
			Dictionary d = (Dictionary) iter.next();
			if(d != null && d.getParentId() != null && parentId.intValue() == d.getParentId().longValue()){
				result.add(d);
			}
		}
		pageContext.setAttribute(var,result);

		return super.doEndTag();
	}
}
